﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using XRM.Security.DataAccess.Enums;
using XRM.Security.DataAccess.Helpers;
using XRM.Security.WebUI.Models.Request;
using XRM.Security.WebUI.Models.SystemUser;
using XRM.Security.WebUI.Models.UserRegister;
using db = XRM.Security.DataAccess.Models;

namespace XRM.Security.WebUI.Models.UserHistory
{
    public class UserHistoryModel : BaseViewModel
    {
        public UserHistoryModel()
        {
        }

        public UserHistoryModel(db.UserHistory dbEntity)
        {
            if (dbEntity != null)
            {
                Id = dbEntity.UserHistoryId;

                UserRegisterId = dbEntity.UserRegisterId;
                GetUserRegister(dbEntity.UserRegister);

                SystemUserId = dbEntity.SystemUserId;
                SystemUserName = dbEntity.SystemUser.ShortName();
                GetSystemUser(dbEntity.SystemUser);

                RequestId = dbEntity.RequestId;
                GetRequest(dbEntity.Request);

                HistoryDate = dbEntity.HistoryDate;
                HistoryText = dbEntity.HistoryText;
                HistoryGroup = (UserHistoryGroup)dbEntity.HistoryGroup;
            }
        }

        public void GetUserRegister(db.UserRegister dbEntity)
        {
            UserRegister = new UserRegisterModel(dbEntity);
        }
        public void GetSystemUser(db.SystemUser dbEntity)
        {
            SystemUser = new SystemUserModel(dbEntity);
        }
        public void GetRequest(db.Request dbEntity)
        {
            Request = new RequestModel(dbEntity);
        }

        public int UserRegisterId { get; set; }
        public UserRegisterModel UserRegister { get; set; }

        public int SystemUserId { get; set; }
        public string SystemUserName { get; set; }
        public SystemUserModel SystemUser { get; set; }

        public int? RequestId { get; set; }
        public RequestModel Request { get; set; }

        public DateTime HistoryDate { get; set; }
        public string HistoryText { get; set; }

        private string pSmallText;
        public string SmallText 
        { 
            get
            {
                return pSmallText;
            }
            set
            {
                pSmallText = value;

                HistoryText = "";
                if (Request != null)
                    HistoryText += "Заявка " + Request.Number.ToString() + " ";
                if (SystemUser != null)
                    HistoryText += SystemUser.FullName + " ";
                HistoryText += value;
            }
        }

        public UserHistoryGroup HistoryGroup;

        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            yield break;
        }
    }
}